/*******************************************************************************
 * 程序说明
 * 
 ******************************************************************************/
#include "../lib/inc/stm8s.h"
#include "../lib/inc/stm8s_clk.h"
#include "../lib/inc/stm8s_gpio.h"

/*******************************************************
 * 休眠1ms
 *******************************************************/
void DelayMS(u16 count)
{
	int j;
	while (count--)
	{
		j = 150;
		while (j--);
	}
}

/*******************************************************
 * 休眠当1时,20um;10时,48um;100时,315um
 *******************************************************/
void DelayUS(u8 count)
{
	while (count--);
}

/*******************************************************
 * 时钟初始化
 *******************************************************/
void CLK_Initialize()
{
	CLK_HSICmd(ENABLE);
	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
}

/*******************************************************
 * GPIO引脚初始化
 *******************************************************/
void GPIO_Initialize()
{
	GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_SLOW);
}

#define LCD_CS		GPIO_PIN_0 
#define LCD_RD		GPIO_PIN_1
#define LCD_RW		GPIO_PIN_2
#define LCD_DATA	GPIO_PIN_3

#define LCD_BUSY_BIT 7

// LCD引脚定义
#define CS0()	GPIO_WriteLow(GPIOB,LCD_CS)
#define CS1()	GPIO_WriteHigh(GPIOB,LCD_CS)
#define RD0()	GPIO_WriteLow(GPIOB,LCD_RD)
#define RD1()	GPIO_WriteHigh(GPIOB,LCD_RD)
#define RW0()	GPIO_WriteLow(GPIOB,LCD_RW)
#define RW1()	GPIO_WriteHigh(GPIOB,LCD_RW)
#define DATA0()	GPIO_WriteLow(GPIOB,LCD_DATA)
#define DATA1()	GPIO_WriteHigh(GPIOB,LCD_DATA)

u8 lcd_read_byte()
{
	u8 i, temp1, temp2;
	temp1 = temp2 = 0;
	for (i = 0; i < 8; i++)
	{
		temp1 = temp1 << 1;
		RD1();
		if (GPIO_ReadInputPin(GPIOB, LCD_RW))
			temp1++;
		RD0();
	}
	for (i = 0; i < 8; i++)
	{
		temp2 = temp2 << 1;
		RD1();
		if (GPIO_ReadInputPin(GPIOB, LCD_RW))
			temp2++;
		RD0();
	}
	return ((0xF0 & temp1) + (0x0F & temp2));
}

void lcd_write_byte(u8 data)
{
	u8 i;
	for (i = 0; i < 8; i++)
	{
		RW1();
		if (data & 0x80)
		{
			DATA1();
		}
		else
		{
			DATA0();
		}
		RW0();
		data = data << 1;
	}
}

void lcd_check_busy()
{
//	do
//	{
//		lcd_write_byte(0xFC); //11111,RW(1),RS(0),0
//	} while (0x80 & lcd_read_byte()); //BF(.7)=1 Busy
	DelayMS(20);
}

void lcd_write_data(u8 data)
{
	CS1();
	lcd_check_busy();
	lcd_write_byte(0xA0); //10100000
	lcd_write_byte(0xF0 & data); //高4位
	lcd_write_byte(0xF0 & data << 4); //低4位
	CS0();
}

void lcd_write_command(u8 command)
{
	CS1();
	lcd_check_busy();
	lcd_write_byte(0xF8); //11111-RW(0)-RS(0)-0
	lcd_write_byte(0xF0 & command); //高4位
	lcd_write_byte(0xF0 & command << 4); //低4位
	CS0();
}

void main(void)
{
	CLK_Initialize();
	GPIO_Initialize();

	for (int i = 0; i < 12; i++)
	{
		GPIO_WriteReverse(GPIOD, GPIO_PIN_0);
		DelayMS(80);
	}

	/* Infinite loop */
	while (1)
	{
		for (u8 i = 0; i < 16; i++)
			lcd_write_data(i);
		DelayMS(100);
        //2015年12月27日，没有说明书，搞不定。
	}
}

#ifdef USE_FULL_ASSERT

void assert_failed(u8* file, u32 line)
{
	while (1)
	{
	}
}
#endif


/************************ (C) COPYRIGHT xknife.net *************END OF FILE****/

